﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Clipping.Utils
{
    public class NewsParser2
    {
        public static void GetContent(Uri url, int pageNo, IHtmlDownload htmlDownloader, out string content, out string channel, out Uri nextPage)
        {
            string html = "";
            string orihtml = "";
            try
            {
                html = htmlDownloader.GetHtml(url);
                orihtml = html;
            }
            catch (Exception ex)
            {
                content = null;
                nextPage = null;
                channel = "";
            }
            //removeUselessLabel(ref html);
            //formatHtml(ref html);
            string formatedHtml = formatHtml(html);
            string html2 = findContentPart(formatedHtml);
            content = formatcontent(html2, url);
            channel = findChannel(html);
            nextPage = getNextPage(orihtml, pageNo, url);


        }

        private static Uri getNextPage(string orihtml, int pageNo, Uri url)
        {
            throw new NotImplementedException();
        }

        private static string findChannel(string html)
        {
            throw new NotImplementedException();
        }

        private static string formatcontent(string html2, Uri url)
        {
            throw new NotImplementedException();
        }

        private static string findContentPart(string html)
        {
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);

            string titleInHead;

            var liNav = doc.CreateNavigator();

            titleInHead = TryGetTitleInHead(liNav);

            string title = TryGetH1(liNav, titleInHead);
            GetAllElements(liNav);
            return null;
        }

        /// <summary>
        /// 格式化HTML
        /// </summary>
        /// <param name="html"></param>
        private static string formatHtml(string html)
        {
            if (string.IsNullOrEmpty(html))
                return html;
            html = Regex.Replace(html, "&nbsp;", " ", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "<div\\s+[^>]*>", "<div>", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "[\\r\\n]", "");
            html = Regex.Replace(html, ">\\s*", ">\r\n");
            html = Regex.Replace(html, "\\s*<", "\r\n<");
            html = Regex.Replace(html, "<\\r\\n", "&lt;");
            html = Regex.Replace(html, "\\r\\n>", "&gt;");
            html = Regex.Replace(html, "(?:<br[^>]*>|<p(>|[^>]*>)|</p>)\\s*", "\r\n", RegexOptions.IgnoreCase);
            return html;
        }
        private static string TryGetTitleInHead(System.Xml.XPath.XPathNavigator liNav)
        {
            string titleInHead = null;
            var titleInHeadNav = liNav.SelectSingleNode("html/head/title");
            if (titleInHeadNav != null)
            {
                titleInHead = titleInHeadNav.Value;
            }
            return titleInHead;
        }

        private static string TryGetH1(System.Xml.XPath.XPathNavigator liNav, string titleInHead)
        {
            var h1Nav = liNav.Select("//h1");

            if (h1Nav.Count == 1)
            {
                while (h1Nav.MoveNext())
                    return h1Nav.Current.Value;
            }
            if (!string.IsNullOrEmpty(titleInHead))
            {
                while (h1Nav.MoveNext())
                {
                    if (titleInHead.Contains(h1Nav.Current.Value))
                    {
                        return h1Nav.Current.Value;
                    }
                }
            }
            return null;
        }
        private static void GetAllElements(System.Xml.XPath.XPathNavigator liNav)
        {

            var bodyNav = liNav.SelectSingleNode("html/body");

            var any = bodyNav.Select("descendant::*");
            List<Element> elements = new List<Element>();
            while (any.MoveNext())
            {
                if (!string.IsNullOrEmpty(any.Current.Value))
                {
                    Element e = new Element() { Type = any.Current.Name, Value = any.Current.OuterXml };
                    elements.Add(e);
                }
            }
        }


        public class Element
        {
            public string Type { get; set; }
            public string Value { get; set; }
        }
    }
}
